	********************************************************************************
	****
	**** Date: 04.25.2022							 
	**** Update: 07.12.2023					 
	**** Author: JW josephgwright@gmail.com 				 	
	**** NOTE: This program has been executed in Stata 18.0	 
	**** 
	**** Using data files in $dir: 
	****
	****  	pers-use.dta
	**** 	termlimit.dta
	****
	*********************************************************************************
	
		capture log close
		log using Ch5.log, replace

	******************************
	**** Set directory, seed *****
	******************************
		set more off 
		set matsize 1000
		global seed ="984353"
		set scheme plotplain
		cd "$dir"

	********************************
	**** Load data, vars trans *****
	********************************
		use pers-use,clear
   		qui reg v2x_polyarchy  create ld if persparty~=.
		gen sample = e(sample)==1
		egen c=count(year) if e(sample)==1,by(lid) 
	 
		* Variables and transformations *
		gen election =  v2xel_elecparl==1 | v2xel_elecpres==1
		gen seat50 = v2paseats>49 if v2paseats~=.
 		gen time = year-1990
 		
		* Standardize outcome variables *
		local var = "vburcap v2lgotovst v2lginvstp v2juhcind v2xlg_legcon v2x_jucon"
		foreach v of local var {
			qui sum `v'
			qui replace `v'=`v'-r(mean)
			qui sum `v'
			qui replace `v'=`v'/r(sd)
		}
 
		sum vburcap v2lgotovst v2lginvstp v2juhcind v2xlg_legcon v2x_jucon
		* Initial levels * Note year before election that brings leader to power
		local var = "polar v2xps_party vburcap v2lgotovst v2lginvstp v2xlg_legcon v2x_jucon v2juhcind "
		foreach v of local var {
			qui tsset cowcode year
			qui gen o`v'=(l2`v') if minyr==year
			qui egen i`v'=max(o`v'),by(lid)
			qui gen d`v'=d.`v'
		}
		
		* Globals *
		global d="persparty"
	    global x1="ld ivdem time"		
		
		* Strong autocorrelation decreases with differenced DV *
		local var = "v2xlg_legcon v2lginvstp v2x_jucon v2juhcind vburcap v2lgotovst"
		foreach v of local var {
			   xtset cowcode year
			   qui prais `v' persparty $x1 i`v' $t, cluster(lid)
					di `e(rho)'
			   qui prais `v' persparty $x1 l1`v' $t, cluster(lid)
					di `e(rho)'
 			   qui prais `v' persparty $x1 l1`v' l2`v' $t, cluster(lid)
					di `e(rho)'
 		 }
		 
		 **************************************
		 ********* Reported estimates *********
		 **************************************
	* Legislative constraint by party creation (Table 5.1) *
		 tab seat50
 		 reg v2xlg_legcon create,cluster(lid)
		 est store l1
		 reghdfe v2xlg_legcon create,a(cowcode)cluster(lid)
		 est store l2
		 reghdfe v2xlg_legcon create seat50,a(cowcode)cluster(lid)
		 est store l3
		 xi:reghdfe v2xlg_legcon  create if seat50==1,a(cowcode)cluster(lid)
		 est store l4
		 xi:reghdfe v2xlg_legcon  create if seat50==0,a(cowcode)cluster(lid)
		 est store l5
		
 	* Legislative constraint on executive *
 		 reg v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon $d $x1 seat50,cluster(lid)
		 est store c1
 		 reghdfe v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon $d $x1 seat50,a(cowcode)cluster(lid)
		 est store d1
 		 xi:reghdfe v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon i.seat50*$d $x1,a(cowcode)cluster(lid)
		 est store s1
		 interflex v2xlg_legcon $d v2paseat l1v2xlg_legcon l2v2xlg_legcon $x1,fe(cowcode)cluster(lid)nbin(4)
		 mat list r(estBin)
	 	 reghdfe v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon $d $x1 seat50,a(cowcode year)cluster(lid)
		 est store f1
		 * Not different in pres v parl *
		 xi:reghdfe v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon i.pres*$d $x1,a(cowcode)cluster(lid)
		 xi:reghdfe v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon $d $x1 if pres==1,a(cowcode)cluster(lid)
		 xi:reghdfe v2xlg_legcon l1v2xlg_legcon l2v2xlg_legcon $d $x1 if pres==0,a(cowcode)cluster(lid)
		 
 	* Judicial constraint *
		 reg v2x_jucon l1v2x_jucon l2v2x_jucon $d $x1,cluster(lid)
		 est store c2
 		 reghdfe v2x_jucon l1v2x_jucon l2v2x_jucon $d $x1,a(cowcode)cluster(lid)
		 est store d2
  		 xi:reghdfe v2x_jucon l1v2x_jucon l2v2x_jucon i.seat50*$d $x1,a(cowcode)cluster(lid)
		 est store s5
		 interflex v2x_jucon $d v2paseat l1v2x_jucon l2v2x_jucon $x1,fe(cowcode year)cluster(lid)nbin(4)
		 mat list r(estBin)
 		 reghdfe v2x_jucon l1v2x_jucon l2v2x_jucon $d $x1,a(cowcode)cluster(lid)
		 est store f2
 
	* Executive oversight * 
		 reg v2lgotovst l1v2lgotovst l2v2lgotovst $d $x1,cluster(lid)
		 est store c3
 		 reghdfe v2lgotovst l1v2lgotovst l2v2lgotovst $d $x1,a(cowcode)cluster(lid)
		 est store d3
  		 xi:reghdfe v2lgotovst l1v2lgotovst l2v2lgotovst i.seat50*$d $x1,a(cowcode)cluster(lid)
		 est store s9
		 interflex v2lgotovst $d v2paseat l1v2lgotovst l2v2lgotovst $x1,fe(cowcode)cluster(lid)nbin(4)
		 mat list r(estBin)
 		 reghdfe v2lgotovst l1v2lgotovst l2v2lgotovst $d $x1,a(cowcode)cluster(lid)
		 est store f3
	
		 erase .pdf

	**********************
	*** Estimate plots ***
	**********************
			gen e=.
			gen hi=.
			gen lo=.
			gen n =_n
			forval i =1/3   {		
				est restore d`i'
				nlcom _b[$d],post
				mat e = e(b)
				mat v = e(V)
				mat list e
				mat list v
				local b=e[1,1] 
				di `b'
				local se = sqrt(v[1,1])
				di `se'
				replace e =`b' if n==`i'
				replace hi = `b' + 1.96* `se' if n==`i'
				replace lo = `b' - 1.96* `se'  if n==`i'
				local i = `i'+1
			}
		 
 			twoway (rspike hi lo n if n<=3,lcol(gs1)) (scatter e n if n<=3,mcol(gs8)msym(O) ///
				ytit({&beta}{sub:Party Personalist},size(large))yline(0,lcol(red)) ///
				xlab(1 `""Legislative" "constraint""'   2  `""Judicial" "constraint""' ///
				3`""Bureaucratic" "oversight""' ) ///
				xscale(range(0.8 3.2))xtit("")note(95% CI,size(vsmall)pos(8)ring(0))yscale(range(-.41 0.034))  legend(off) ///
				xtit("Outcomes: institutional constraints on the executive",height(6))saving(h1.gph,replace) ///
				title(Party personalism reduces institutional constraints)ylab(-.4(.1)0))
			drop e n hi lo
		
			gen e=.
			gen hi=.
			gen lo=.
			gen n =_n

			local var = "1 5 9"
			foreach i of local var   {	
				est restore s`i'
				nlcom _b[$d],post
				mat e = e(b)
				mat v = e(V)
				mat list e
				mat list v
				local b=e[1,1] 
				di `b'
				local se = sqrt(v[1,1])
				di `se'
				replace e =`b' if n==`i'
				replace hi = `b' + 1.96* `se' if n==`i'
				replace lo = `b' - 1.96* `se'  if n==`i'

				est restore s`i'
				nlcom _b[$d] + _b[_IseaXpersp_1],post
				mat e = e(b)
				mat v = e(V)
				mat list e
				mat list v
				local b=e[1,1] 
				di `b'
				local se = sqrt(v[1,1])
				di `se'
				local j=`i'+1
				replace e =`b' if n==`j'
				replace hi = `b' + 1.96* `se' if n==`j'
				replace lo = `b' - 1.96* `se'  if n==`j'
 			}
			 
 			twoway (rspike hi lo n if n==1 | n==5 | n==9  ,lcol(gs1))  ///
				(rspike hi lo n if n==2 | n==6 | n==10  ,lcol(gs8))  ///
				(scatter e n if n==1 | n==5 | n==9  ,mcol(gs1)msym(T)) ///
				(scatter e n if n==2 | n==6 | n==10  ,mcol(gs8)msym(O) ///
				ytit({&beta}{sub:Party Personalist},size(large))yline(0,lcol(red)) ///
				xlab(1.5 `""Legislative" "constraint""' 5.5   `""Judicial" "constraint""' ///
				9.5 `""Bureaucratic" "oversight""' ) ///
				xscale(range(0.8 10.2))xtit("")note(95% CI,size(vsmall)pos(8)ring(0)) ///
				legend(lab(3 "Ruling party seat share<50%")lab(4 "Ruling party seat share>=50%")pos(6)order(3 4)ring(0)col(2)) ///
				xtit("Outcomes: institutional constraints on the executive",height(6))yscale(range(-.41 0.034)) ///
				title(By legislative majority) ///
				ylab(-.4(.1)0) saving(h2.gph,replace))
  			drop e n hi lo
			gr combine h1.gph h2.gph,xsize(9)tit(Party personalism and institutional constraints on the executive)iscale(.7)
			gr export "$dir\golden\Ch5-Pers-Party-Exec-Constraint.pdf",as(pdf)replace 
			erase h1.gph
			erase h2.gph


			
*************************************
****** Term limit analysis **********
*************************************

			*** Term limit changes ***
			use pers-use,clear
			tsset cowcode year
			gen presvote = l.v2elvotlrg
			replace presvote = l.presvote if presvote==.
			sort country year
			merge country year using termlimit
			tab _merge
			drop if _merge ==2
			replace termlimitattempt =1 if country=="Peru" & year==1993
			replace termlimitfail =0 if country=="Peru" & year==1993
			replace termlimitattempt =1 if country=="Sri Lanka" & year==2010
			replace termlimitfail =0 if country=="Sri Lanka" & year==2010
			recode termlimitfail termlimitattempt (.=0) if year<=2018 & year>=1993
			gen election =v2xel_elecparl==1 |  v2xel_elecpres==1
			local var = "v2x_jucon v2xlg_legcon"
			foreach v of local var {
				gen o`v'=l1`v' if year==min
				egen i`v'=max(o`v'),by(lid)
			}
			xtset cowcode year
			gen l1election = l1.election
			gen l2election = l2.election
			gen f1election = f1.election
			gen f2election = f2.election
			gen ecycle = election==1 | f1election==1
			keep if year>=1993 & year<=2018
			tab country type
			keep if pres==1 | ccode==345
			
			* Bivariate descriptive *
			centile persparty,centile(50)
			gen hip =persparty>=r(c_1) if persparty~=.		
			ttest termlimitatt,by(hip)
				
			 * Variables *
			alpha presvote v2paseat v2pavote,std item gen(prespopular)
			local var = "i_popul persparty ivdem iv2x_jucon iv2xlg_legcon ld"
			foreach v of local var {
				qui sum `v'
				replace `v' = (`v' - abs(r(mean)))/r(sd)
			 
			}
			replace ivdem  =ivdem*10 /* rescale to plot estimate */
			sum i_pop persparty
			replace l1v2x_jucon=l1v2x_jucon*2
			replace l1v2xlg_legcon=l1v2xlg_legcon*2
			gen Election = election==1 | f1election==1
			
			twoway (lpoly termlimitattempt persparty,legend(off)bw(.5))  (lowess termlimitattempt persparty) 
			probit termlimitattempt ld persparty,cluster(lid)

			 * Reported estimates *
			reghdfe termlimitattempt ld persparty,a(cowcode year)cluster(lid)
			est store term1
			gen s = e(sample)==1
			reghdfe termlimitattempt ivdem Election ld persparty,a(cowcode year)cluster(lid)
			est store term2
			reghdfe termlimitattempt ivdem prespopular Election iv2x_jucon iv2xlg_legcon ld persparty,a(cowcode year)cluster(lid)
			est store term3
			reghdfe termlimitattempt i_populism ivdem prespopular Election iv2x_jucon iv2xlg_legcon ld persparty,a(cowcode year)cluster(lid)
			est store term4
			
			 * Plot estimates *
			label var iv2x_jucon `""Judicial  " "constraint""'
			label var iv2xlg_legcon `""Legislative" "constraint ""'
			label var persparty `""{bf:Party}     " "{bf:personalism}""'
			label var ivdem `""Initial    " "democracy" "level     ""'
			label var ld `""Democracy" "age     ""'
			label var Election "Election year"
			label var i_populism `""Party   " "Populism""'
			label var prespopular `""Leader  " "Popularity""'

			coefplot (term1, msymbol(d))(term2, msymbol(P)) (term3, msymbol(T)) (term4, msymbol(Oh)), order(persparty)  ///
				drop(_cons) xline(0) msymbol(d) mfcolor(white) grid(glcolor(gs15)) ///
				levels(95 90) legend(lab(3 "Baseline")lab(6 "+ election + initial democracy level") ///
				lab(9 "+ legislative/judicial constraint")  lab(12 "+ populism")order(3 6 9 12) ///
				size(small) pos(6) col(2) ring(1)) xsize(2) ysize(2) xlab(-.06(.02).04)  ///
				xtitle("        Coefficient estimate", size(small))  ///
				ciopts(lwidth(thin)) aspectratio(1.1) scale(.75) title(Attempts to adjust term limits, size(medium) height(2)) ///
				note("Presidential systems only",pos(6)size(vsmall))
			gr export "$dir\golden\Ch5-Pers-Party-Term-Limit-Attempts.pdf",as(pdf)replace 
	
			* Check with non-linear link *
			xthybrid termlimitattempt persparty ld  if s==1,cluster(cowcode)fam(binomial)link(logit)p
			xthybrid termlimitattempt persparty xperiod* Election ld ivdem  if s==1,cluster(cowcode)fam(binomial)link(logit)p
			xthybrid termlimitattempt persparty xperiod* ivdem prespopular Election iv2x_jucon iv2xlg_legcon ld ///
				if s==1,cluster(cowcode)fam(binomial)link(logit)p
			xthybrid termlimitattempt persparty xperiod* i_popul ivdem prespopular Election iv2x_jucon iv2xlg_legcon ld ///
				if s==1,cluster(cowcode)fam(binomial)link(logit)p
	
	
	** By legislative majority **
	ttest  termlimitattempt  if v2paseat~=. & l1election==0,by(create)
	ttest  termlimitattempt  if v2paseats< 50 & v2paseat~=. & l1election==0,by(create)
	ttest  termlimitattempt  if v2paseats>=50 & v2paseat~=. & l1election==0,by(create)
	
	interflex termlimitattempt persparty v2paseat ld ivdem if l1election==0,fe(cowcode year)cluster(lid)nbin(2)cutoffs(49.9)
	mat list r(estBin)
	interflex termlimitattempt create v2paseat ld ivdem if l1election==0,fe(cowcode year)cluster(lid)nbin(2)cutoffs(49.9)
	mat list r(estBin)
	interflex termlimitattempt persparty v2paseat ld ivdem if l1election==0,fe(cowcode year)cluster(lid)nbin(2)cutoffs(49.9)
	mat list r(estBin)
	interflex termlimitattempt create v2paseat ld ivdem if l1election==0,fe(cowcode year)cluster(lid)nbin(2)cutoffs(49.9)
	mat list r(estBin)
	interflex termlimitattempt persparty v2paseat i_populism ivdem prespopular Election iv2x_jucon iv2xlg_legcon ld ///
		if l1election==0,fe(cowcode year)cluster(lid)cut(49.9)
	mat list r(estBin)
	interflex termlimitattempt create v2paseat i_populism ivdem prespopular Election iv2x_jucon iv2xlg_legcon ld ///
		if l1election==0,fe(cowcode year)cluster(lid)cut(49.9)
	mat list r(estBin)

	* Result reported in online Appendix *
	gen e=.
	gen hi=.
	gen lo=.
	gen n =_n
	interflex termlimitattempt persparty v2paseat ld ivdem if l1election==0,fe(cowcode year)cluster(lid)cut(49.9)
	mat list r(estBin)

	replace e=r(estBin)[1,2] if n==1
	replace e=r(estBin)[2,2] if n==2
	replace hi=r(estBin)[1,5] if n==1
	replace hi=r(estBin)[2,5] if n==2
	replace lo=r(estBin)[1,4] if n==1
	replace lo=r(estBin)[2,4] if n==2
	twoway (rspike lo hi n if n<=2,legend(off)ytit(Marginal effect of ruling party personalism) xtit(Ruling party legislative seat share))  ///
		(scatter e n if n<=2,xlab(1 "<50 %" 2 ">=50%")yline(0)xscale(range(0.8 2.2))saving(h1.gph,replace)tit(Ruling party personalism) ///
		yscale(range(-0.04 .2))ylab(0(.05).2)) 
		
		
	interflex termlimitattempt create v2paseat ld ivdem if l1election==0,fe(cowcode year)cluster(lid)cut(49.9)
	mat list r(estBin)

	replace e=r(estBin)[1,2] if n==1
	replace e=r(estBin)[2,2] if n==2
	replace hi=r(estBin)[1,5] if n==1
	replace hi=r(estBin)[2,5] if n==2
	replace lo=r(estBin)[1,4] if n==1
	replace lo=r(estBin)[2,4] if n==2
	twoway (rspike lo hi n if n<=2,legend(off)ytit(Marginal effect of create-party) xtit(Ruling party legislative seat share))  ///
		(scatter e n if n<=2,xlab(1 "<50 %" 2 ">=50%")yline(0)xscale(range(0.8 2.2))saving(h2.gph,replace)tit(Create party) ///
		yscale(range(-0.04 .2))ylab(0(.05).2)) 
		
	gr combine h1.gph h2.gph,tit(Party personalism increases term limit attempts when ruling party has legislative majority)xsize(8)
	gr export "$dir\golden\T-Term-Limit-Attempts-LegSeatShare.pdf",as(pdf)replace 
	
	erase .pdf
	erase h1.gph
	erase h2.gph

	************  THE END ***********
			
 log close


	